package com.sx.service.design;

import com.sx.basic.BasicService;
import com.sx.basic.IBasicService;
import com.sx.basic.ResponseEntity;
import com.sx.basic.exception.BasicException;
import com.sx.basic.impl.BasicServiceImpl;
import com.sx.basic.pageable.Condition;
import com.sx.design.IDesignModuleService;
import com.sx.design.IDesignParamsService;
import com.sx.entity.design.DesignModuleEntity;
import com.sx.entity.design.DesignParamsEntity;
import com.sx.mapper.design.DesignModuleMapper;
import com.sx.utils.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AdinZ
 * @date 2020/11/12 17:37
 */
@Service
public class DesignModuleService extends BasicServiceImpl<DesignModuleMapper,DesignModuleEntity>
        implements BasicService<DesignModuleEntity> , IDesignModuleService {

    @Resource
    private IDesignParamsService designParamsService;

    /**
     * 更新组件模板
     * @param entity
     * @return
     */
    @Override @Transactional
    public ResponseEntity putDesignModule(DesignModuleEntity entity) {
        if (entity == null) return toError();
        List<DesignParamsEntity> list = entity.getList();
        if (entity.getId() != null) {
            this.designParamsService.deleteByModuleId(entity.getId());
        } else {
            this.save(entity);
        }
        if (list != null && list.size() > 0)
            this.designParamsService.putParams(entity.getId(), list);
        this.redisService.delete(DesignModuleEntity.KEY,entity.getId());
        return toSuccess();
    }

    @Override
    public DesignModuleEntity getDetail(Long id) {
        DesignModuleEntity entity=this.redisService.get(DesignModuleEntity.KEY,id);
        if(entity==null) {
            entity = get(id);
            if (entity == null) return null;
            List<DesignParamsEntity> list = this.designParamsService.findTree(entity.getId());
            entity.setList(list);
            this.redisService.set(DesignModuleEntity.KEY,entity.getId(),entity);
        }
        return entity;
    }


}
